package com.hxh.user.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author huangxunhui
 * Date: Created in 18/9/30 下午4:59
 * Utils: Intellij Idea
 * Description:设置cookie
 */
public class CookieUtil {

    public static void set(HttpServletResponse response , String name , String value , int maxAge){
        Cookie cookie = new Cookie(name , value);
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }

    public static Cookie get(HttpServletRequest request , String name ){
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie : cookies) {
                if(name.equals(cookie.getName())){
                    return cookie;
                }
            }
        }
        return null;
    }

}
